﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：周传雄 
    //
    // 文件名：DistributePlanList.cs
    // 文件功能描述：发票管理
    // 创建标识：周传雄2009-05-12
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Data.SqlClient;
using HZ.Common;

namespace HZ.DrugTrade.Web.Enterprise.InvoiceManage
{
    public partial class InvoiceDistribution : BasePage
    {

        public InvoiceDistribution()
        {
            this.ModuleNo = Module.发票管理.ToString();
        }

        string invoiceSerialID = "";
        string hositalID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            invoiceSerialID = Request.QueryString["InvoiceSerialID"].ToString();
            hositalID = Request["hID"].ToString();

            if (string.IsNullOrEmpty(invoiceSerialID) || string.IsNullOrEmpty(hositalID))
            {
                Response.Write("<script> alert('非正常操作！'); history.go(-2);</script>");
            }
            else if (!IsPostBack)
            {
                QueryResult();
            }
        }

        /// <summary>
        /// 配送详情
        /// </summary>
        private void QueryResult()
        {

            IList<Model.View.Distribution> LisDistribution = new List<Model.View.Distribution>();
            Model.Query.Distribution qryCondition = new Model.Query.Distribution();
            User user = CurrentAccount.GetUser();
            qryCondition.CompanyID_PS = user.Org.OrgID;
            qryCondition.OrderName = txtOrderName.Text.ToString().Trim();
            //qryCondition.HospitalName = txtHospitalName.Text.ToString().Trim();
            qryCondition.DistributeTimeFrom = new HZ.Common.ConvertHelper(txtTimeFrom.Text.ToString().Trim()).ToDateTime;
            qryCondition.DistributeTimeTo = new HZ.Common.ConvertHelper(txtTimeTo.Text.ToString().Trim()).ToDateTime;
            qryCondition.GoodsName = txtGoodsName.Text.Trim().ToString();
            qryCondition.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            qryCondition.Outlookc = txtOutLookc.Text.ToString().Trim();
            qryCondition.CompanyName_SC = txtCompanyName.Text.ToString().Trim();
            qryCondition.ProjectID = this.ddlProjectid.SelectedValue;
            qryCondition.HospitalID = hositalID;
            //qryCondition.OrderDetailState = 2;
            qryCondition.DistributeState = 1;
            qryCondition.Append = "distributionserialid not in (select distributionserialid from Tb_Invoicedetail)";
            qryCondition.SortFields = " DistributeTime desc";
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IBLL_Purchase iPurchase = Factory.BLL_Purchase();
            LisDistribution = iPurchase.GetPurchaseOrderDetail_DistributeNoInvoiceByWhere(qryCondition, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwInvoice.DataSource = LisDistribution;
            gvwInvoice.DataBind();
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            QueryResult();
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            QueryResult();
        }
        /// <summary>
        /// 添加发票明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>      

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            IList<Distribution> LisDistribution = new List<Distribution>();
            IBLL_Purchase iPurchase = Factory.BLL_Purchase();

            string[] DistributionSerialIDs = Request.Form["ChkOrderID"].ToString().Split(',');


            foreach (var item in DistributionSerialIDs)
            {
                LisDistribution.Add(new Distribution() { DistributionSerialID = item });
            }
            Invoice invoice = new Invoice() { InvoiceSerialID = invoiceSerialID };

            int nSuccess = iPurchase.AddInvoiceDistribution(invoice, LisDistribution);

            if (nSuccess == 1)
            {
                HZ.Web.WebUtil.Alert("添加成功!", this);
                //Response.Write("<script> window.opener.location.href=window.opener.location.href;</script>");
            }
            else if (nSuccess == 2)
            {
                HZ.Web.WebUtil.Alert("发票已经提交不能加入明细");
            }
            else if (nSuccess == 3)
            {
                HZ.Web.WebUtil.Alert("有产品正在退货中，不能建立发票！");
            }
            else
            {
                HZ.Web.WebUtil.Alert("添加失败", this);
            }
            QueryResult();
        }

        /// <summary>
        /// 返回到发票
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFinish_Click(object sender, EventArgs e)
        {
            HZ.Web.WebUtil.RedirectByScript("InvoiceAdd.aspx?InvoiceSerialID=" + Request.QueryString["InvoiceSerialID"] + "&hID=" + Request.QueryString["hID"]);
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            Model.Query.Distribution qryCondition = new Model.Query.Distribution();
            User user = CurrentAccount.GetUser();
            qryCondition.CompanyID_PS = user.Org.OrgID;
            qryCondition.OrderName = txtOrderName.Text.ToString().Trim();
            qryCondition.DistributeTimeFrom = new HZ.Common.ConvertHelper(txtTimeFrom.Text.ToString().Trim()).ToDateTime;
            qryCondition.DistributeTimeTo = new HZ.Common.ConvertHelper(txtTimeTo.Text.ToString().Trim()).ToDateTime;
            qryCondition.GoodsName = txtGoodsName.Text.Trim().ToString();
            qryCondition.Outlookc = txtOutLookc.Text.ToString().Trim();
            qryCondition.CompanyName_SC = txtCompanyName.Text.ToString().Trim();
            qryCondition.HospitalID = hositalID;
            qryCondition.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            qryCondition.ProjectID = this.ddlProjectid.SelectedValue;
            qryCondition.DistributeState = 1;
            qryCondition.Append = "distributionserialid not in (select distributionserialid from Tb_Invoicedetail)";
            qryCondition.SortFields = " DistributeTime desc";
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.Distribution>(qryCondition, true);
            string sql = string.Format(@"SELECT OrderName 采购单名称,HospitalName 采购医疗机构,CompanyName_SC 生产企业,GoodsName 产品名称,Outlookc 规格型号,
                                        Material 材质,Price 采购价格,PurchaseCount 采购数量,Dis_DistributeCount 批配送量,Dis_WarehouseCount 批入库量,
                                        Settlecount 可结算量,SettleAmount 可结算金额,BatchRecordID 批号,DistributeTime 配送时间 FROM View_Distribution WHERE 1=1 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "产品信息.xls");
        }


    }
}
